package com.fram.service.api.v1.android.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fram.bean.admin.android.goods.request.GoodsAndroidListRequest;
import com.fram.bean.admin.android.goods.request.GoodsAndroidRequest;
import com.fram.bean.admin.android.goods.request.GoodsSalesRequest;
import com.fram.bean.admin.android.goods.response.GoodsAndroidListResponse;
import com.fram.bean.admin.android.goods.response.GoodsSalesResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.qiniu.QiniuUtils;
import com.fram.entity.ProjectGoods;
import com.fram.entity.ProjectGoodsPrice;
import com.fram.mapper.ProjectGoodsMapper;
import com.fram.mapper.ProjectGoodsPriceMapper;
import com.fram.service.api.v1.android.GoodsAndroidService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class GoodsAndroidServiceImpl implements GoodsAndroidService{
	
	@Autowired
	private ProjectGoodsMapper projectGoodsMapper;
	@Autowired
	private ProjectGoodsPriceMapper projectGoodsPriceMapper;
	@Autowired
	private QiniuUtils qiniuUtils;
	
	@Override
	public Long add(GoodsAndroidRequest request) throws ProException {
		if(request.getName().length() > 20) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "标题不可超过20个字");
		}
		if (!StringUtils.isEmpty(request.getLogo())) {
			request.setLogo(qiniuUtils.uploadImageUrl(request.getLogo()));
		}
		if (!StringUtils.isEmpty(request.getImg())) {
			request.setImg(qiniuUtils.uploadImageUrl(request.getImg()));
		}
		request.setIntroduce(qiniuUtils.dealHtml(request.getIntroduce(), "1/" + request.getDistributorId() + "/"));
		request.setAfterSaleService(qiniuUtils.dealHtml(request.getAfterSaleService(), "1/" + request.getDistributorId() + "/"));
		request.setProductFeatures(qiniuUtils.dealHtml(request.getProductFeatures(), "1/" + request.getDistributorId() + "/"));
		if(projectGoodsMapper.insertSelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		for (ProjectGoodsPrice price : request.getPriceList()) {
			price.setGoodsId(request.getId());
			if(projectGoodsPriceMapper.insertSelective(price)<=0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
		return request.getId();
	}

	@Override
	public PageInfo<GoodsAndroidListResponse> list(PageBean<GoodsAndroidListRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<GoodsAndroidListResponse> list = projectGoodsMapper.listForAndroid(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public void update(GoodsAndroidRequest request) throws ProException {
		if (!StringUtils.isEmpty(request.getLogo())) {
			request.setLogo(qiniuUtils.uploadImageUrl(request.getLogo()));
		}
		if (!StringUtils.isEmpty(request.getImg())) {
			request.setImg(qiniuUtils.uploadImageUrl(request.getImg()));
		}
		//"<p>""</p>"
		request.setIntroduce(qiniuUtils.dealHtml(request.getIntroduce(), "1/" + request.getDistributorId() + "/"));
		request.setAfterSaleService(qiniuUtils.dealHtml(request.getAfterSaleService(), "1/" + request.getDistributorId() + "/"));
		request.setProductFeatures(qiniuUtils.dealHtml(request.getProductFeatures(), "1/" + request.getDistributorId() + "/"));
		if(projectGoodsMapper.updateByPrimaryKeySelective(request)<=0){
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public ProjectGoods detail(Long id) throws ProException {
		ProjectGoods detail= projectGoodsMapper.selectByIdAndroid(id);
		if(detail == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return detail;
	}

	@Override
	public ProjectGoodsPrice priceDetail(Long id) throws ProException {
		ProjectGoodsPrice price=projectGoodsPriceMapper.selectByPrimaryKey(id);
		if(price == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return price;
	}

	@Override
	public void priceUpdate(ProjectGoodsPrice request) throws ProException {
		if(projectGoodsPriceMapper.updateByPrimaryKeySelective(request)<=0){
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
	
	//增加多个规格
	@Override
	public void priceAdd(List<ProjectGoodsPrice> request) throws ProException {
		for (ProjectGoodsPrice projectGoodsPrice : request) {
			if(projectGoodsPriceMapper.insertSelective(projectGoodsPrice)<=0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
	}

	@Override
	public List<GoodsSalesResponse> sales(GoodsSalesRequest request) throws ProException {
		List<GoodsSalesResponse>  list=projectGoodsMapper.goodsSales(request);
		if(list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	public void priceUpdateList(List<ProjectGoodsPrice> request) throws ProException {
		
	}
}
